/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package br.com.jteam.jfcm.model;

import br.com.jteam.jfcm.model.util.FileTypeUtil;

/**
 * <P>
 * Enum que representa os tipos de arquivos renderiz&aacute;veis pela
 * aplica&ccedil;&atilde;o.
 * 
 * @author Rafael Naufal <rafael.naufal@gmail.com>
 * @since 1.0
 * @version $Id$
 */
public enum FileType
{
		/**
		 * <p>
		 * Tipos de arquivos que podem ser exibidos pelo sistema. O tipo
		 * <code>DEFAULT</code> serve apenas para construir um arquivo
		 * padr&atilde;o.
		 */
		DEFAULT(
			" "),
		JPG(
			".jpg"),
		PNG(
			".png"),
		GIF(
			".gif"),
		TEXT(
			".txt"),
		ZIP(
			".zip"),
		JAVA(
			".java"),
		XML(
			".xml"),
		XHTML(
			".xhtml"),
		PROPERTIES(
			".properties"),
		HTML(
			".html"),
		SHELL_SCRIPT(
			".sh");
	/**
	 * <p>
	 * Representa o tipo da extens&atilde;o do arquivo.
	 */
	private String fileExtension;

	/**
	 * <p>
	 * Construtor que recebe o tipo da extens&atilde;o do arquivo.
	 * 
	 * @param fileExtension -
	 *            tipo de extens&atilde;o do arquivo.
	 * @throws IllegalArgumentException
	 *             caso a extens&atilde;o do arquivo seja nula.
	 */
	private FileType(String fileExtension)
	{
		if (fileExtension == null)
		{
			throw new IllegalArgumentException("File extension cannot be null.");
		}
		this.fileExtension = fileExtension;
	}

	/**
	 * <p>
	 * Retorna o tipo de extens&atilde;o de arquivo do enum.
	 * 
	 * @return - tipo de extens&atilde;o do arquivo.
	 */
	public String getFileExtension()
	{
		return fileExtension;
	}

	/**
	 * <p>
	 * Retorna o tipo de arquivo do <code>enum</code> a partir do nome
	 * arquivo.
	 * 
	 * @param fileName -
	 *            nome do arquivo
	 * @return tipo do arquivo. Se nenhum mapeamento para um tipo de arquivo for
	 *         encontrado, retorna nulo.
	 * @throws IllegalArgumentException
	 *             caso o nome do arquivo seja nulo.
	 */
	public static FileType getFileTypeByFileName(String fileName)
	{
		if (fileName == null)
		{
			throw new IllegalArgumentException("File name cannot be null.");
		}
		String extension = FileTypeUtil.extractFileExtension(fileName);
		for (FileType fileType : FileType.values())
		{
			if (fileType.getFileExtension().equalsIgnoreCase(extension))
			{
				return fileType;
			}
		}
		return null;
	}
}
